/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.persistence.hibernate.core;

import com.blandware.atleap.common.util.PartialCollection;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.core.ApplicationResource;
import com.blandware.atleap.persistence.core.ApplicationResourceDAO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>Hibernate implementation of ApplicationResourceDAO</p>
 * <p><a href="ApplicationResourceDAOHibernate.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.6 $ $Date: 2008/06/23 13:24:53 $
 */
public class ApplicationResourceDAOHibernate extends BaseDAOHibernate implements ApplicationResourceDAO {

	/**
	 * Creates new instance of ApplicationResourceDAOHibernate
	 */
	public ApplicationResourceDAOHibernate() {
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#createApplicationResource(com.blandware.atleap.model.core.ApplicationResource)
	 */
	public void createApplicationResource(ApplicationResource applicationResource) {
		getHibernateTemplate().save(applicationResource);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#retrieveApplicationResource(com.blandware.atleap.model.core.ApplicationResource.ApplicationResourceID)
	 */
	public ApplicationResource retrieveApplicationResource(ApplicationResource.ApplicationResourceID id) {
		return (ApplicationResource) getHibernateTemplate().get(ApplicationResource.class, id);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#updateApplicationResource(com.blandware.atleap.model.core.ApplicationResource)
	 */
	public void updateApplicationResource(ApplicationResource applicationResource) {
		getHibernateTemplate().saveOrUpdate(applicationResource);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#deleteApplicationResource(com.blandware.atleap.model.core.ApplicationResource)
	 */
	public void deleteApplicationResource(ApplicationResource applicationResource) {
		getHibernateTemplate().delete(applicationResource);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ApplicationResourceDAO#listApplicationResources(com.blandware.atleap.common.util.QueryInfo)
	 */
	public PartialCollection listApplicationResources(QueryInfo queryInfo) {
		String whereClause = new String();
		String orderByClause = new String();
		if ( queryInfo != null ) {
			whereClause = queryInfo.getWhereClause();
			orderByClause = queryInfo.getOrderByClause();
			if ( whereClause == null ) {
				whereClause = new String();
			}
			if ( orderByClause != null && orderByClause.length() != 0 ) {
				orderByClause = " order by " + orderByClause;
			} else {
				orderByClause = " order by r.id.key";
			}
		}


		ArrayList args = new ArrayList();
		String localeIdentifier = null;
		if ( queryInfo != null ) {
			Map queryParameters = queryInfo.getQueryParameters();
			localeIdentifier = (String) queryParameters.get("localeIdentifier");
		}

		if ( localeIdentifier != null ) {
			if ( whereClause.length() > 0 ) {
				whereClause += " and";
			}
			whereClause += " (r.id.locale.identifier = ? or r.id.locale.defaultInstance = 'T')";
			args.add(localeIdentifier);
		}

		if ( whereClause.trim().length() > 0 ) {
			whereClause = " where " + whereClause;
		}

		List list = null;
		Integer total = null;
		String hqlPart = "from ApplicationResource r" + whereClause;
		if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
			String hqlForTotal = "select count(distinct r.id.key) " + hqlPart;
			total = findUniqueIntegerResult(hqlForTotal, args.toArray());
			if ( total == null ) {
				total = new Integer(0);
			}
		}
        // If we don't have any info about the total number of results yet or
        // we know that there's something that will be found, then fetch data
		if ( total == null || total.intValue() > 0 ) {
			String hql = "select r " + hqlPart + orderByClause;
			list = executeFind(hql, queryInfo, args.toArray());
			if ( total == null ) {
				total = new Integer(list.size());
			}
		} else {
			list = new ArrayList();
		}
		return new PartialCollection(list, total);
	}

}
